package cn.hnucm.userservice.controller;

import cn.hnucm.userservice.entity.*;
import cn.hnucm.userservice.service.TbLogService;
import cn.hnucm.userservice.service.TbUserBasisService;
import cn.hnucm.userservice.service.TbUserSignService;
import cn.hnucm.userservice.utils.IPUtils;
import cn.hnucm.userservice.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户登录的表 前端控制器
 * </p>
 *
 * @author zjl
 * @since 2021-12-08
 */
@Api(tags = "tb_User_Sign类")
@RestController
@RequestMapping("/userservice/tbUserSign")
public class TbUserSignController {

    @Autowired
    private TbUserSignService tbUserSignService;
    @Autowired
    private TbUserBasisService tbUserBasisService;
    @Autowired
    private TbLogService tbLogService;
    @Autowired
    private HttpServletRequest req;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");



    /**
     * 路径： /userservice/tbUserSign/getUserSign/1
     * 描述: 根据userId查询tb_user_sign表
     *
     * @param userId 用户id
     * @return List<TbUserSign>
     */

    @GetMapping("/getUserSignById/{userId}")
    public List<TbUserSign> getUserSignById(@PathVariable("userId") int userId) {
        long startTime = System.currentTimeMillis();
        List<TbUserSign> tbUserSignList = tbUserSignService.findTbUserSignById(userId);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tbUserSignList.size() == 0) {
            userid = 0;
        } else {
            userid = userId;
        }
        TbLog tbLog = new TbLog(Integer.parseInt(req.getHeader("userId")), "查找用户登录信息", "getUserSignById", "{userId:" + userId + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        tbLogService.addTbLog(tbLog);
        return tbUserSignList;
    }
//
//    /**
//     * 路径： /userservice/tbUserSign/getUserSignByAccount/2223334441
//     * 描述: 根据userAccount查询tb_user_sign表
//     *
//     * @param userAccount 账号
//     * @return  List<TbUserSign>
//     */
//
//    @GetMapping("/getUserSignByAccount/{userAccount}")
//    public List<TbUserSign> getUserSignByAccount(@PathVariable("userAccount") String userAccount) {
//        QueryWrapper<TbUserSign> tbUserSignQueryWrapper = new QueryWrapper<>();
//        tbUserSignQueryWrapper.eq("userAccount", userAccount);
//        return tbUserSignService.list(tbUserSignQueryWrapper);
//    }
//

    /**
     * 路径： /userservice/tbUserSign/getUserBasisMessage/2223334441
     * 描述: 根据userAccount查询tb_user_sign表和tb_user_basis表
     *
     * @param userAccount 账号
     * @return List<TbUserBasicMessage>
     */
    @PostMapping("/getUserBasisMessage/{userAccount}")
    public List<TbUserBasicMessage> getUserBasisMessage(@PathVariable("userAccount") String userAccount) {
        long startTime = System.currentTimeMillis();
        List<TbUserBasicMessage> tbUserBasicMessageList = tbUserSignService.findTbUserBasicMessageByAccount(userAccount);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tbUserBasicMessageList.size() == 0) {
            userid = 0;
        } else {
            userid = tbUserBasicMessageList.get(0).getUserId();
        }
        TbLog tbLog = new TbLog(userid, "查找用户登录信息和个人信息", "getUserBasisMessage", "{userAccount:" + userAccount + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        tbLogService.addTbLog(tbLog);
        return tbUserBasicMessageList;
    }

    /**
     * 路径： /userservice/tbUserSign/addUserSign
     * 描述: 向tb_user_sign表添加usersign，注册用户的接口
     *
     * 12
     * @param tbUserSign 用户登录信息
     * @return boolean
     */

    @PostMapping("/addUserSign")
    public boolean addUserSign(@RequestBody TbUserSign tbUserSign) {
        List<TbUserBasicMessage> tbUserBasicMessageList = tbUserSignService.findTbUserBasicMessageByAccount(tbUserSign.getUserAccount());
        if (tbUserBasicMessageList.size() != 0) {
            return false;
        }
        long startTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        tbUserSign.setUserCreateTime(dateFormat.format(d));
        tbUserSign.setUserIdentity("user");
        boolean tSign = tbUserSignService.addTbUserSign(tbUserSign);
        TbUserBasis tbUserBasis = new TbUserBasis();
        tbUserBasis.setUserUsername("user_" + tbUserSign.getUserId());
        boolean tBasis = tbUserBasisService.addTbUserBasis(tbUserBasis);
        long endTime = System.currentTimeMillis();
        int userid;
        if (tSign == true) {
            userid = tbUserSign.getUserId();
        } else {
            userid = 0;
        }
        TbLog tbLog = new TbLog(userid, "注册用户", "addUserSign", "{TbUserSign:" + tbUserSign + ", TbUserBasis:" + tbUserBasis + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        //System.out.println(tbLog);
        tbLogService.addTbLog(tbLog);
        return tSign;
    }

    /**
     * 路径： /userservice/tbUserSign/updateUserSign
     * 描述: 根据userId更新tb_user_sign表的数据
     *
     * @param tbUserSign 用户登录信息
     * @return boolean
     */

    @PostMapping("/updateUserSign")
    public boolean updateUserSign(@RequestBody TbUserSign tbUserSign) {
        long startTime = System.currentTimeMillis();
        boolean tSign = tbUserSignService.updateTbUserSign(tbUserSign);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tSign == true) {
            userid = tbUserSign.getUserId();
        } else {
            userid = 0;
        }
        TbLog tbLog = new TbLog(Integer.parseInt(req.getHeader("userId")), "更新用户登录信息", "updateUserSign", "{TbUserSign:" + tbUserSign + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        //System.out.println(tbLog);
        tbLogService.addTbLog(tbLog);
        return tbUserSignService.updateTbUserSign(tbUserSign);
    }

    /**
     * 路径： /userservice/tbUserSign/deleteUserSign
     * 描述: 根据userId删除tb_user_sign表的数据
     *
     * @param userId 用户id
     * @return boolean
     */

    @PostMapping("/deleteUserSign")
    public boolean deleteUserSign(@RequestBody int userId) {
        long startTime = System.currentTimeMillis();
        boolean tSign = tbUserSignService.deleteTbUserSign(userId);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        TbLog tbLog = new TbLog(Integer.parseInt(req.getHeader("userId")), "删除用户登录信息", "deleteUserSign", "{userId:" + userId + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        //System.out.println(tbLog);
        tbLogService.addTbLog(tbLog);
        return tSign;
    }
}

